package com.example.pathprotectsystem.service.impl;

import com.example.pathprotectsystem.mapper.UserMapper;
import com.example.pathprotectsystem.pojo.User;
import com.example.pathprotectsystem.pojo.Password;
import com.example.pathprotectsystem.service.UserService;
import com.example.pathprotectsystem.utils.Md5Util;
import com.example.pathprotectsystem.utils.ThreadLocalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Override
    public User findByUserName(String username) {
        User user = userMapper.findByUserName(username);
        return user;
    }
    @Override
    public void register(String username, String password) {
        //加密
        String md5String = Md5Util.getMD5String(password);
        //添加用户
        userMapper.add(username,md5String,"普通用户");
    }
    @Override
    public void update(User user) {
        userMapper.update(user);
    }
    @Override
    public void updatePwd(Password password) {
        Map<String, Object> map = ThreadLocalUtil.get();
        Integer id = (Integer) map.get("id");
        password.setId(id);
        password.setNewPassword(Md5Util.getMD5String(password.getRePassword()));
        userMapper.updatePwd(password);
    }
    @Override
    public void updateavatar(User user) {
        userMapper.updateavatar(user);
    }
    @Override
    public List<User> listpart(String str) {
        List<User> p = userMapper.listpart(str);
        return p;
    }
    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public void initPassWord(Integer id) {
        String initWord = Md5Util.getMD5String("123456");
        userMapper.initPassWord(initWord,id);
    }

    @Override
    public List<String> getpersons() {
        List<String> p = userMapper.getpersons();
        return p;
    }

    @Override
    public int findUserIdByUserName(String userName) {
        int userid = userMapper.findUserIdByUserName(userName);
        return userid;
    }


}
